package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysProduct;
import com.ruoyi.system.mapper.SysProductMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysSupplierProductMapper;
import com.ruoyi.system.domain.SysSupplierProduct;
import com.ruoyi.system.service.ISysSupplierProductService;

/**
 * 供应商对应经营的产品Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-22
 */
@Service
public class SysSupplierProductServiceImpl implements ISysSupplierProductService
{
    @Autowired
    private SysSupplierProductMapper sysSupplierProductMapper;
    @Autowired
    private SysProductMapper sysProductMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询供应商对应经营的产品
     *
     * @param supplierId 供应商对应经营的产品主键
     * @return 供应商对应经营的产品
     */
    @Override
    public List<SysSupplierProduct> selectSysSupplierProductBySupplierId(Long supplierId)
    {
        return sysSupplierProductMapper.selectSysSupplierProductBySupplierId(supplierId);
    }

    /**
     * 查询供应商对应经营的产品列表
     *
     * @param sysSupplierProduct 供应商对应经营的产品
     * @return 供应商对应经营的产品
     */
    @Override
    public List<SysSupplierProduct> selectSysSupplierProductList(SysSupplierProduct sysSupplierProduct)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = sysUserMapper.selectUserById(loginUser.getUser().getUserId());
        sysSupplierProduct.setSupplierId(user.getSupplierId());
        return sysSupplierProductMapper.selectSysSupplierProductList(sysSupplierProduct);
    }

    /**
     * 新增供应商对应经营的产品
     *
     * @param sysSupplierProduct 供应商对应经营的产品
     * @return 结果
     */
    @Override
    public int insertSysSupplierProduct(SysSupplierProduct sysSupplierProduct)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = sysUserMapper.selectUserById(loginUser.getUserId());
        sysSupplierProduct.setSupplierId(user.getSupplierId());
        return sysSupplierProductMapper.insertSysSupplierProduct(sysSupplierProduct);
    }

    /**
     * 修改供应商对应经营的产品
     *
     * @param sysSupplierProduct 供应商对应经营的产品
     * @return 结果
     */
    @Override
    public int updateSysSupplierProduct(SysSupplierProduct sysSupplierProduct)
    {
        return sysSupplierProductMapper.updateSysSupplierProduct(sysSupplierProduct);
    }

    /**
     * 批量删除供应商对应经营的产品
     *
     * @param supplierIds 需要删除的供应商对应经营的产品主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierProductBySupplierIds(Long[] supplierIds)
    {
        return sysSupplierProductMapper.deleteSysSupplierProductBySupplierIds(supplierIds);
    }

    /**
     * 删除供应商对应经营的产品信息
     *
     * @param supplierId 供应商对应经营的产品主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierProductBySupplierId(Long supplierId)
    {
        return sysSupplierProductMapper.deleteSysSupplierProductBySupplierId(supplierId);
    }

    @Override
    public List<SysProduct> getProductList(Long supplierId) {
        List<SysSupplierProduct> list = sysSupplierProductMapper.selectSysSupplierProductBySupplierId(supplierId);
        List<SysProduct> products = new ArrayList<>();
        for (SysSupplierProduct sysSupplierProduct : list) {
            Long proId = sysSupplierProduct.getProId();
            SysProduct sysProduct = sysProductMapper.selectSysProductByProId(proId);
            products.add(sysProduct);
        }
        return products;
    }

}
